<?php

namespace App\Filament\Resources\Card\CardCodesResource\Pages;

use App\Events\UserDataAdded;
use App\Filament\Resources\Card\CardCodesResource;
use App\Models\CardCodes;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;

class CreateCardCodes extends CreateRecord
{
    protected static string $resource = CardCodesResource::class;

    protected function mutateFormDataBeforeCreate(array $data): array
    {
        $data['user_id'] = auth()->id();
        $data['is_used'] = false;
        return parent::mutateFormDataBeforeCreate($data); // TODO: Change the autogenerated stub
    }


    protected function handleRecordCreation(array $data): Model
    {
        $quantity = $data['quantity'];
        $cardCodeData = [];
        for ($i = 1;$i<=$quantity;$i++){
            unset($data['quantity']);
            $cardCodeData[] = $data;
        }
        UserDataAdded::dispatch($cardCodeData);
        return CardCodes::query()->latest()->first();
    }
}
